/**
 * AddressBookAction.java
 * 
 * 
 * lssrc.com
 * 2013-01-12
 */
package com.lssrc.pams.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;

import com.lssrc.pams.domain.AddressBook;
import com.lssrc.pams.domain.Message;
import com.lssrc.pams.domain.MessageState;
import com.lssrc.pams.domain.PamsUser;
import com.lssrc.pams.service.AddressBookService;
import com.lssrc.pams.service.MessageService;
import com.lssrc.pams.service.MessageStateService;
import com.lssrc.pams.util.PamsConstant;
import com.lssrc.pams.util.UtilFactory;
import com.opensymphony.xwork2.ActionSupport;

@Controller("addressBookAction")
@Scope("prototype")
public class AddressBookAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private AddressBookService addressBookService;
	private MessageService messageService;
	private MessageStateService messageStateService;
	private String msg;
	private int beginIndex;
	private int count;
	private String friendUsername;
	private List<AddressBook> addressBooks;

	public String applyFriend() {
		PamsUser user = this.getUser();
		PamsUser friend = new PamsUser(friendUsername);
		if(addressBookService.findFriendByUser(user, friend) != null) {
			msg = "[{result:'success'}]"; 
			return SUCCESS;
		}
		String applyTime = UtilFactory.getDateUtil().getRightTime();
		msg = addressBookService.save(new AddressBook(user, friend, applyTime, PamsConstant.FRIEND_ING)) ? "[{result:'success'}]" : "[{result:'error'}]";
		Message message = new Message(UtilFactory.getUuidUtil().getUUID(), "添加好友申请", applyTime, user.getUsername() + "添加" + friendUsername + "好友申请", user);
		msg = messageService.save(message) ? "[{result:'success'}]" : "[{result:'error'}]";
		msg = messageStateService.save(new MessageState(message, friend, PamsConstant.MESSAGE_READ_NO, applyTime)) ? "[{result:'success'}]" : "[{result:'error'}]";
		return SUCCESS;
	}
	
	public String beFriend() {
		String beTime = UtilFactory.getDateUtil().getRightTime();
		PamsUser user = this.getUser();
		PamsUser friend = new PamsUser(friendUsername);
		msg = addressBookService.save(new AddressBook(user, friend, beTime, PamsConstant.FRIEND_YES)) ? "[{result:'success'}]" : "[{result:'error'}]";
		msg = addressBookService.update(new AddressBook(friend, user, beTime, PamsConstant.FRIEND_YES)) ? "[{result:'success'}]" : "[{result:'error'}]";
		return SUCCESS;
	}

	public String queryByUser() {
		Long rowCount;
		String dataMsg = "[]";
		PamsUser pamsUser = this.getUser();
		try {
			rowCount = addressBookService.getCountByUser(pamsUser);
			addressBooks = addressBookService.findByUser(pamsUser, beginIndex, count);
			if (addressBooks.size() > 0) {
				dataMsg = "[";
				for (AddressBook addressBook : addressBooks) {
					pamsUser = addressBook.getFriend();
					dataMsg += "{";
					dataMsg += "name:'" + pamsUser.getUsername() + "',";
					dataMsg += "tel:'" + pamsUser.getTelephone() + "',";
					dataMsg += "email:'" + pamsUser.getEmail() + "',";
					dataMsg += "qq:'" + pamsUser.getQq() + "'";
					dataMsg += "},";
				}
				dataMsg = dataMsg.substring(0, dataMsg.length() - 1);
				dataMsg += "]";
			}
			msg = "{result:[{resultMsg:'success',rowCount:'" + rowCount +"'}], data:" + dataMsg + "}";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "{result:[{resultMsg:'error',errorMsg:'错误'}], 'data':[]}";
		}
		return SUCCESS;
	}
	
	public String delete() {
		msg = addressBookService.delete(new AddressBook(this.getUser(), new PamsUser(friendUsername))) ? "[{result:'success'}]" : "[{result:'error'}]";
		return SUCCESS;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public PamsUser getUser() {
		String username;
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
			return new PamsUser(username);
		}
		return null;
	}

	public String getFriendUsername() {
		return friendUsername;
	}

	public void setFriendUsername(String friendUsername) {
		this.friendUsername = friendUsername;
	}

	public List<AddressBook> getAddressBooks() {
		return addressBooks;
	}

	public void setAddressBooks(List<AddressBook> addressBooks) {
		this.addressBooks = addressBooks;
	}

	@Resource
	public void setAddressBookService(AddressBookService addressBookService) {
		this.addressBookService = addressBookService;
	}

	@Resource
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	@Resource
	public void setMessageStateService(MessageStateService messageStateService) {
		this.messageStateService = messageStateService;
	}

}
